package cn.cau.control;

import cn.cau.dao.AuditingApplyRecordsDao;
import cn.cau.dao.AuditingTopInfoDao;
import cn.cau.entity.*;
import cn.cau.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class CodeAuditingGetController {
    /*
    编码审核
     */

    @Autowired
    private AuditingListService auditingListService;

    @Autowired
    private AuditingTopService auditingTopService;

    @Autowired
    private AuditingMidService auditingMidService;

    @Autowired
    private AuditingBotService auditingBotService;

    @Autowired
    private CategoryService categoryService;


    @RequestMapping(value = "/auditing", method = RequestMethod.GET)     //到审核主页的路由
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/codeAuditing/codeAuditingIndex");
        return modelAndView;
    }

    @RequestMapping(value = "/auditing/list", method = RequestMethod.GET)     //到待处理审核事项路由
    public ModelAndView list() {

        List<AuditingApplyRecord> records = new ArrayList<>();   //总待审核表
        //首先从apply表中获取待审核记录
        List<CodeManageRecord> designApplies = auditingListService.getAllUnDoRecord();
        for(CodeManageRecord apply : designApplies){
            AuditingApplyRecord temp_record = new AuditingApplyRecord();
            int old_id = apply.getOldCodeRuleId(),new_id = apply.getNewCodeRuleId();
            CodeRule rule = new CodeRule();

            int op = apply.getManageOperation();
            if(op==0){
                temp_record.setOperation("修改申请");
            }
            else if(op==1){
                temp_record.setOperation("废除申请");
            }
            else{
                temp_record.setOperation("设计申请");
            }

            if(op==1){
                rule = auditingListService.getReluById(old_id);  //如果为废除申请，则对old_rule进行操作
            }
            else{
                rule = auditingListService.getReluById(new_id);  //否则，对new_rule进行操作
            }

            if(rule==null){
                continue;
            }

            //todo
            //待新增状态、申请操作等信息

            temp_record.setIndex(apply.getCodeRuleManageId());
            temp_record.setReason(apply.getManageContent());
            temp_record.setOp(op);    //0代表修改，1代表设计，2代表废除
            temp_record.setState(rule.getCodeState());
            temp_record.setOldId(apply.getOldCodeRuleId());
            temp_record.setNewId(apply.getNewCodeRuleId());     //newId中存储申请对应的主表id
            temp_record.setDate(apply.getApplyTime());         //日期
            temp_record.setUserId(apply.getManageManId());       //申请人
            temp_record.setObjectName(rule.getCodeRuleName());  //获取名称
            records.add(temp_record);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("records",records);
        modelAndView.setViewName("/codeAuditing/codeAuditingList");
        return modelAndView;
    }

    @RequestMapping(value = "/auditingTop",method = RequestMethod.GET)            //到整体审核的路由
    public ModelAndView RedirToTopAudit(@RequestParam(value = "id") int id){        //传递的id为record的id
        ModelAndView modelAndView = new ModelAndView();

        CodeManageRecord apply = auditingListService.findRecordByManageId(id);
        CodeRule rule = new CodeRule();
        int rongyu = 1;
        if(apply.getManageOperation()==1){
            rule = auditingListService.getReluById(apply.getOldCodeRuleId());
        }
        else{
            rule= auditingListService.getReluById(apply.getNewCodeRuleId());
        }


        String cateName = categoryService.findCategoryById(rule.getCategoryId()).getCategory();


        AuditingUtils utils = new AuditingUtils();
        utils.setCodeId(rule.getRuleCategoryId());
        utils.setSegName(cateName);

        System.out.println(utils);

        List<CodeRule> pre_rules = auditingTopService.getRuleByCateIdAndName(utils);

        for(CodeRule p_rule:pre_rules){
            if(p_rule.getCodeState()==2){
                rongyu=0;
            }
        }

        modelAndView.addObject("CateName",cateName);
        modelAndView.addObject("rongyuflag",rongyu);
        modelAndView.addObject("record",apply);
        modelAndView.addObject("rule",rule);
        modelAndView.setViewName("/codeAuditing/codeAuditingTop");
        //todo查找出id所对应的记录，添加到模型中，模型跳转到整体审核页面
        return modelAndView;
    }

    @RequestMapping(value = "/auditingMid")    //到码段审核的路由
    public ModelAndView RedirToMidAudit(@RequestParam(value = "id") int id){        //传递的id为record的id
        ModelAndView modelAndView = new ModelAndView();

        CodeManageRecord apply = auditingListService.findRecordByManageId(id);
        CodeRule rule= auditingListService.getReluById(apply.getNewCodeRuleId());


        List<NonEnum> nonEnums = auditingMidService.findAllNoneEnumByCodeId(rule.getCodeRuleId());        //查找该id对应的所有非枚举
        modelAndView.addObject("rule",rule);
        modelAndView.addObject("nonEnums",nonEnums);
        modelAndView.addObject("record",apply);
        modelAndView.setViewName("/codeAuditing/codeAuditingMid");
        //todo查找出id所对应的记录，添加到模型中，模型跳转到整体审核页面
        return modelAndView;
    }

    @RequestMapping(value = "/auditingBot")    //到码节点审核的路由
    public ModelAndView RedirToBotAudit(@RequestParam(value = "id") int id){    //传递的为record的id
        ModelAndView modelAndView = new ModelAndView();

        CodeManageRecord apply = auditingListService.findRecordByManageId(id);
        CodeRule rule= auditingListService.getReluById(apply.getNewCodeRuleId());

        List<Options> enums = auditingBotService.findAllEnumByCodeId(rule.getCodeRuleId());          //查找该id对应的所有枚举类型,并且以option的方式返回

        System.out.println(enums);
        modelAndView.addObject("rule",rule);
        modelAndView.addObject("enums",enums);     //找出该rule中的所有枚举类型
        modelAndView.addObject("record",apply);
        modelAndView.setViewName("/codeAuditing/codeAuditingBot");
        return modelAndView;
    }



    @RequestMapping(value = "/auditingInfo")    //到信息生成的路由
    public ModelAndView RedirToInfo(@RequestParam(value = "id") int id){        //同样，这里需要传递的是record的id
        ModelAndView modelAndView = new ModelAndView();

        int flag = 1;

        CodeManageRecord apply = auditingListService.findRecordByManageId(id);
        CodeRule rule= auditingListService.getReluById(apply.getNewCodeRuleId());

        AuditingTopInfo topInfo = auditingTopService.getTopAuditingInfoByCodeId(apply.getNewCodeRuleId()).get(0);  //总是返回id最大的，即最新的顶层信息
        List<AuditingMidInfo> midInfos = auditingMidService.findAuditingMidInfoByCodeId(apply.getNewCodeRuleId());
        List<AuditingBotInfo> botInfos = auditingBotService.findAuditingBotInfoByCodeId(apply.getNewCodeRuleId());

        System.out.println(topInfo);
        if(topInfo.getIsPass()==0){
            flag = 0;
        }

        for(AuditingMidInfo info: midInfos){
            System.out.println(info);
            if(info.getIsPass()==0){
                flag=0;
            }
        }

        for(AuditingBotInfo info: botInfos){
            System.out.println(info);
            if(info.getIsPass()==0){
                flag=0;
            }
        }


        modelAndView.addObject("record",apply);    //这种record后面更换成gnb和zfy的表单
        modelAndView.addObject("rule",rule);
        modelAndView.addObject("topInfo",topInfo);
        modelAndView.addObject("midInfos",midInfos);
        modelAndView.addObject("botInfos",botInfos);

        modelAndView.addObject("Pass",flag);

        modelAndView.setViewName("/codeAuditing/codeAuditingInfo");
        return modelAndView;
    }


    @RequestMapping(value = "/auditing/hislist", method = RequestMethod.GET)     //到待处理审核事项路由
    public ModelAndView HisList() {

        List<AuditingApplyRecord> records = new ArrayList<>();   //总待审核表
        //首先从apply表中获取待已审核记录
        List<CodeManageRecord> designApplies = auditingListService.getAllDoRecord();

        for(CodeManageRecord apply : designApplies){
            AuditingApplyRecord temp_record = new AuditingApplyRecord();
            int old_id = apply.getOldCodeRuleId(),new_id = apply.getNewCodeRuleId();
            CodeRule rule = new CodeRule();

            int op = apply.getManageOperation();
            if(op==0){
                temp_record.setOperation("修改申请");
            }
            else if(op==1){
                temp_record.setOperation("废除申请");
            }
            else{
                temp_record.setOperation("设计申请");
            }

            if(op==1){
                rule = auditingListService.getReluById(old_id);  //如果为废除申请，则对old_rule进行操作
            }
            else{
                rule = auditingListService.getReluById(new_id);  //否则，对new_rule进行操作
            }

            if(rule==null){
                continue;
            }
            temp_record.setResult(apply.getManageResult());
            temp_record.setIndex(apply.getCodeRuleManageId());
            temp_record.setReason(apply.getManageContent());
            temp_record.setOp(op);    //0代表修改，1代表设计，2代表废除
            temp_record.setState(rule.getCodeState());
            temp_record.setOldId(apply.getOldCodeRuleId());
            temp_record.setNewId(apply.getNewCodeRuleId());     //newId中存储申请对应的主表id
            temp_record.setDate(apply.getApplyTime());         //日期
            temp_record.setUserId(apply.getManageManId());       //申请人
            temp_record.setObjectName(rule.getCodeRuleName());  //获取名称
            records.add(temp_record);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("records",records);
        modelAndView.setViewName("/codeAuditing/codeAuditingListHis");
        return modelAndView;
    }


    @RequestMapping(value = "/auditingInfoShow")    //到审核信息页面的路由
    public ModelAndView RedirToInfoShow(@RequestParam(value = "id") int id){        //同样，这里需要传递的是record的id
        ModelAndView modelAndView = new ModelAndView();

        int flag = 1;

        CodeManageRecord apply = auditingListService.findRecordByManageId(id);
        CodeRule rule= new CodeRule();
        if(apply.getManageOperation()==1){
            rule = auditingListService.getReluById(apply.getOldCodeRuleId());
        }

        else{
            rule= auditingListService.getReluById(apply.getNewCodeRuleId());
        }

        AuditingTopInfo topInfo = auditingTopService.getTopAuditingInfoByCodeId(rule.getCodeRuleId()).get(0);  //总是返回id最大的，即最新的顶层信息
        List<AuditingMidInfo> midInfos = auditingMidService.findAuditingMidInfoByCodeId(rule.getCodeRuleId());
        List<AuditingBotInfo> botInfos = auditingBotService.findAuditingBotInfoByCodeId(rule.getCodeRuleId());

        System.out.println(topInfo);
        if(topInfo.getIsPass()==0){
            flag = 0;
        }

        for(AuditingMidInfo info: midInfos){
            System.out.println(info);
            if(info.getIsPass()==0){
                flag=0;
            }
        }

        for(AuditingBotInfo info: botInfos){
            System.out.println(info);
            if(info.getIsPass()==0){
                flag=0;
            }
        }

        modelAndView.addObject("record",apply);    //这种record后面更换成gnb和zfy的表单
        modelAndView.addObject("rule",rule);
        modelAndView.addObject("topInfo",topInfo);
        modelAndView.addObject("midInfos",midInfos);
        modelAndView.addObject("botInfos",botInfos);

        modelAndView.addObject("Pass",flag);

        modelAndView.setViewName("/codeAuditing/codeAuditingInfoShow");

        return modelAndView;
    }
}
